//二级菜单级联

var chu_project_work = {

	project: "select1",
	work: "select2",
	url1: "",
	url2: "",
	//初始化
	//wrapID :   包裹三级分类的标签ID
	//category1： 省ID 对应 Value
	//category2：     市ID 对应 Value
	//category3：   县ID 对应 Value
	//useEmpty： 是否支持请选择,如果为false则默认三级都加载
	//successCallBack：加载完成后的回调函数
	Init: function(wrapID, useEmpty, successCallBack) {
		chu_project_work.InitTag(wrapID, useEmpty);
		chu_project_work.InitData(useEmpty);
		chu_project_work.category1Select(useEmpty);
		chu_project_work.category2Select(useEmpty);
	},

	//初始化标签
	InitTag: function(wrapID, useEmpty) {
		var tmpInit = "";
		tmpInit += "<span class='category_span'>项目</span>";
		if(useEmpty) {
			tmpInit += "<select id='" + chu_project_work.project + "' name='" + chu_project_work.project + "'><option value='0'>--请选择--</option></select>";
		} else {
			tmpInit += "<select id='" + chu_project_work.project + "' name='" + chu_project_work.project + "'></select>";
		}
		tmpInit += "<span class='category_span'>工点</span>";
		if(useEmpty) {
			tmpInit += "<select id='" + chu_project_work.work + "' name='" + chu_project_work.work + "'><option value='0'>--请选择--</option></select>";
		} else {
			tmpInit += "<select id='" + chu_project_work.work + "' name='" + chu_project_work.work + "'></select>";
		}

		$("#" + wrapID + "").html(tmpInit);
	},
	//初始化数据--包括修改
	InitData: function(useEmpty) {

		$.get("select.json", {}, function(data) {
			var firstItem = data[0].value;
			//初始化一级分类
			for(var i = 0; i < data.length; i++) {
				var tmp_option = " <option value='" + data[i].value + "'>" + data[i].display + "</option>";
				$("#" + chu_project_work.project + "").html($("#" + chu_project_work.project + "").html() + tmp_option);
			}

			if(useEmpty) {
				return;
			}

			/* //初始化二级分类
		                $.get(url2, { pid: firstItem }, function (dataChild) {
		      
		                    for (var i = 0; i < category2.length; i++) {
		                        var tmp_option = " <option value='" + category2[i].Value + "'>" + category2[i].Display + "</option>";
		                        $("#" + chu_project_work.work + "").append(tmp_option); 
		                    }
		                }, "json"); 
*/

		}, "json")

	},

	//一级分类change
	category1Select: function(useEmpty) {
		$("#" + chu_project_work.project + "").change(function() {
			var optionHtml = "";
			if(useEmpty) {
				optionHtml = "<option value='0'>--请选择--</option>";
			}
			$("#" + chu_project_work.work + "").html(optionHtml);

			var tmpSelectedcategory1 = $("#" + chu_project_work.project + " option:selected").val();
			//初始化二级分类
			$.get(chu_project_work.url2, {
				pid: tmpSelectedcategory1
			}, function(dataChild) {

				for(var i = 0; i < dataChild.length; i++) {
					var tmp_option = " <option value='" + dataChild[i].value + "'>" + dataChild[i].display + "</option>";
					$("#" + chu_project_work.work + "").html($("#" + chu_project_work.work + "").html() + tmp_option);
				}
				if(useEmpty) {
					return;
				}

			}, "json");

		});
	},

	//二级分类change
	category2Select: function(useEmpty, successCallBack) {
		$("#" + chu_project_work.work + "").change(function() {
			var optionHtml = "";
			if(useEmpty) {
				optionHtml = "<option value='0'>--请选择--</option>";
				$("#" + chu_project_work.Category3ID + "").html(optionHtml);
			} else {
				var sv1 = $("#" + chu_project_work.project + " option:selected").val();
				var sv2 = $("#" + chu_project_work.work + " option:selected").val();
				successCallBack(sv1, sv2);
			}
		});
	}

}